home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / scheme / scm / xscm.lha / xscm / xmsubs.scm < prev    next >
Encoding:
Text File  |  1992-08-29  |  7.8 KB  |  239 lines

  1. ; $Header: /home/campbell/Languages/Scheme/scm/x-scm/RCS/xmsubs.scm,v 1.5 1992/07/26 21:29:14 campbell Beta $
  2. ;
  3. ; Generally useful Motif functions.  These are EXTREMELY subject to 
  4. ; change.  I intend to try to create more-or-less toolkit-independent
  5. ; versions of these, so that I can build compatible versions for
  6. ; OpenLook, Microsoft Windows, Macintosh, etc.  So be prepared for
  7. ; these interfaces to change...  -lc
  8. ;
  9. ;  Author: Larry Campbell (campbell@redsox.bsw.com)
  10. ;  Copyright 1992 by The Boston Software Works, Inc.
  11. ;  Permission to use for any purpose whatsoever granted, as long
  12. ;  as this copyright notice remains intact.  Please send bug fixes
  13. ;  or enhancements to the above email address.
  14.  
  15. (require (in-vicinity (library-vicinity) "assert.scm"))
  16.  
  17. ; Call a thunk with a "busy" cursor (watch, hourglass, glacier...)
  18. ;
  19. (define (with-busy-cursor widget thunk)
  20.   (if (xt:is-realized widget)
  21.       (let ((xdisplay (xt:display widget))
  22.         (xwindow (xt:window widget)))
  23.     (x:define-cursor xdisplay xwindow xc:watch)
  24.     (x:flush xdisplay)
  25.     (apply thunk '())
  26.     (x:undefine-cursor xdisplay xwindow))
  27.       (apply thunk '())))
  28.  
  29. ; Create a text widget with a caption to its left.  Returns the
  30. ; text widget's ID.
  31. ;
  32. (define (make-captioned-text-widget parent label columns . args)
  33.   #.(assert '(string? label))
  34.   #.(assert '(integer? columns))
  35.   (let* ((rc (xt:create-managed-widget
  36.           "ct" xm:form parent))
  37.      (caption (xt:create-managed-widget
  38.            "caption" xm:label-gadget rc
  39.            xm:n-label-string (xm:string-create label)))
  40.      (text (xt:create-managed-widget
  41.         "text" xm:text-field rc
  42.         xm:n-columns columns))
  43.      (offset (+
  44.           (xt:get-value text xm:n-shadow-thickness xt:integer)
  45.           (xt:get-value text xm:n-highlight-thickness xt:integer)
  46.           (xt:get-value text xm:n-margin-height xt:integer))))
  47.     (xt:set-values
  48.      caption
  49.      xm:n-margin-height offset
  50.      xm:n-right-attachment xm:attach-widget
  51.      xm:n-bottom-attachment xm:attach-form
  52.      xm:n-right-widget text)
  53.     (xt:set-values
  54.      text
  55.      xm:n-right-attachment xm:attach-form
  56.      xm:n-bottom-attachment xm:attach-form
  57.      xm:n-right-widget text)
  58.     text))
  59.  
  60. (define (popup-error parent message)
  61.   #.(assert '(string? message))
  62.   (let* ((dshell (xt:create-popup-shell
  63.           "Error" xm:dialog-shell parent))
  64.      (mshell (xt:create-managed-widget
  65.           "Error" xm:message-box dshell
  66.           xm:n-dialog-type xm:dialog-error
  67.           xm:n-message-string (xm:string-create message))))
  68.     (xt:add-callback
  69.      mshell
  70.      xm:n-ok-callback (lambda (w) (xt:destroy-widget dshell)))
  71.     (xt:popup dshell 1)))
  72.  
  73. (define (popup-information parent message)
  74.   #.(assert '(string? message))
  75.   (let* ((dshell (xt:create-popup-shell
  76.           "Information" xm:dialog-shell parent))
  77.      (mshell (xt:create-managed-widget
  78.           "Information" xm:message-box dshell
  79.           xm:n-dialog-type xm:dialog-information
  80.           xm:n-message-string (xm:string-create message))))
  81.     (xt:add-callback
  82.      mshell
  83.      xm:n-ok-callback (lambda (w) (xt:destroy-widget dshell)))
  84.     (xt:popup dshell 1)))
  85.  
  86.  
  87. ; Create a row of evenly-spaced buttons (typically used for the
  88. ; "OK" "Apply" "Cancel" buttons at the bottom of a panel).
  89. ; Returns nothing.
  90. ; Usage:
  91. ;   (make-button-row parent '(("label 1" action1) ("label 2" action2)))
  92. ;
  93. (define (make-button-row parent button-specifiers)
  94.   #.(assert '(list? button-specifiers))
  95.   (let ((rc (xt:create-managed-widget
  96.          "rc" xm:row-column parent
  97.          xm:n-orientation xm:horizontal
  98.          xm:n-packing xm:pack-column))
  99.     (parent-width (xt:get-value parent xt:n-width xt:integer)))
  100.     (if (=? 0 parent-width)
  101.     (error "button-row: parent has zero width"))
  102.     (do ((items button-specifiers (cdr items)))
  103.     ((null? items) rc)
  104.       (let* ((item (car items))
  105.          (label (car item))
  106.          (action (cadr item))
  107.          (others (cddr item)))
  108.     (apply make-button `(,label ,rc ,action ,@others))))))
  109.  
  110.  
  111. (define (make-button label parent action . args)
  112. ;;
  113. ;; Make a button.  If <action> is a list, the button pops up a pulldown
  114. ;; menu, and <action> is the argument list for make-pulldown-menu.
  115. ;; If <label> begins with a question mark, the question mark is removed,
  116. ;; and the button is a toggle button.
  117. ;;
  118.   #.(assert '(or (symbol? label) (string? label)))
  119.   #.(assert
  120.      '(or
  121.        (procedure? action)
  122.        (list? action))
  123.      'action)
  124.   (let ((widget '())
  125.     (widget-callback (if (null? args) args (car args)))
  126.     (args (if (null? args) args (cdr args)))
  127.     (class '())
  128.     (callback xm:n-activate-callback))
  129.     (set! widget
  130.       (if (list? action)
  131.           (apply make-pulldown-menu `(,label ,parent ,@action))
  132.           (begin
  133.         (case label
  134.           ((xm:arrow-up xm:arrow-down xm:arrow-left xm:arrow-right)
  135.            (set! widget
  136.              (xt:create-managed-widget
  137.               "button" xm:arrow-button-gadget parent
  138.               xm:n-arrow-direction
  139.               (case label
  140.                 ((xm:arrow-down) xm:arrow-down)
  141.                 ((xm:arrow-up) xm:arrow-up)
  142.                 ((xm:arrow-left) xm:arrow-left)
  143.                 ((xm:arrow-right) xm:arrow-right))
  144.               xm:n-traversal-on #f)))
  145.  
  146.           (else
  147.            (let ((class xm:push-button-gadget))
  148.              (if (char=? (string-ref label 0) #\?)
  149.              (begin
  150.                (set! class xm:toggle-button-gadget)
  151.                (set! callback xm:n-value-changed-callback)
  152.                (set! label
  153.                  (substring label 1 (string-length label)))))
  154.              (set! widget
  155.                (xt:create-managed-widget
  156.                 label class parent
  157.                 xm:n-alignment xm:alignment-center
  158.                 xm:n-shadow-thickness 2)))))
  159.         (xt:add-callback widget callback action)
  160.         (or (null? args)
  161.             (apply xt:set-values `(,widget ,@args)))
  162.         widget)))
  163.     (if (not (null? widget-callback))
  164.     (widget-callback widget))
  165.     widget))
  166.  
  167. (define (make-toggle-button label parent action . resources)
  168.   #.(assert '(string? label))
  169.   #.(assert '(procedure? action))
  170.   (let ((widget
  171.      (apply xt:create-managed-widget
  172.       `(,label
  173.         ,xm:toggle-button-gadget
  174.         ,parent
  175.         ,@resources))))
  176.     (xt:add-callback widget xm:n-value-changed-callback action)
  177.     widget))
  178.  
  179. ; (make-popup-menu name parent (label1 action1) (label2 action2)...)
  180. ;
  181. (define (make-popup-menu name parent . args)
  182.   (let* ((widget (xm:create-popup-menu parent name)))
  183.     (xt:create-managed-widget name xm:label-gadget widget)
  184.     (xt:create-managed-widget name xm:separator-gadget widget)
  185.     (do ((items args (cdr items)))
  186.     ((null? items) widget)
  187.         (let* ((item (car items))
  188.                   (label (car item))
  189.            (action (cadr item)))
  190.       (make-button label widget action)))))
  191.  
  192. ; (make-pulldown-menu name parent (label1 action1 wc) (label2 action2 wc)...)
  193. ;
  194. ; wc is an optional argument -- if present, it must be a procedure
  195. ; of one argument which is called with the widget representing the
  196. ; button created.
  197. ;
  198. (define (make-pulldown-menu name parent . args)
  199.   #.(assert '(string? name) 'name 'args)
  200.   #.(assert '(< 1 (length args)) 'name 'args)
  201.   (let* ((mbutton (xt:create-managed-widget
  202.            name xm:cascade-button-gadget parent))
  203.      (menu-pane (xm:create-pulldown-menu parent name)))
  204.     (xt:set-values mbutton xm:n-sub-menu-id menu-pane)
  205.     (do ((items args (cdr items)))
  206.     ((null? items) mbutton)
  207.       (let* ((item (car items))
  208.          (label (car item))
  209.          (action (cadr item))
  210.          (widget-callback
  211.           (if (= 3 (length item))
  212.           (list-ref item 2)
  213.           '())))
  214.     (make-button label menu-pane action widget-callback)))))
  215.  
  216. ; (make-menu-bar parent name ((menu1-title ((label action) ...)) ...)
  217. ;
  218. (define (make-menu-bar parent name . args)
  219.   #.(assert '(string? name) 'name 'args)
  220.   #.(assert '(< 1 (length args)) 'name 'args)
  221.   (let ((menubar (xt:create-managed-widget
  222.           name xm:row-column parent
  223.           xm:n-row-column-type xm:menu-bar)))
  224.     (do ((items args (cdr items)))
  225.     ((null? items) menubar)
  226.       (let* ((item (car items))
  227.          (menu-title (car item))
  228.          (menu-items (cadr item))
  229.          (widget ()))
  230.     (set! widget (apply
  231.               make-pulldown-menu
  232.               `(,menu-title
  233.             ,menubar
  234.             ,@menu-items)))
  235.     (if (equal? menu-title "Help")
  236.         (xt:set-values menubar xm:n-menu-help-widget widget))))))
  237.